<?php

namespace App\Dao;

use App\Exceptions\SysExceptions;
use App\Model\CursorsModel;

class CursorsDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
        $query = CursorsModel::query();

        $query->when(checkIsHasField($params,'cursors_id'),function ($query)use ($params){
            $query->where('cursors_id',$params['cursors_id']);
        });

        $query->when(checkIsHasField($params,'show'),function ($query)use ($params){
            $query->where('show',$params['show']);
        });

        return $query->orderByDesc('created_at');
    }


    /**
     * @throws SysExceptions
     */
    public static function add($params = [])
    {
        if(!self::builder_query()->create($params)){
            self::error('添加失败!');
        }
    }

    public static function cursors(array $all)
    {
        return self::builder_query($all)->paginate();
    }

    public static function cursorsWithNoPage(array $array)
    {
        return self::builder_query($array)->get()->toArray();
    }

    public static function cursors_info(array $array): array
    {
        return self::builder_query($array)->first()->toArray();
    }

    public static function edit(array $params)
    {
        if(self::builder_query(['cursors_id'=>self::deleteParamId($params,'cursors_id')])->update(self::filterFields($params,new CursorsModel())) === false){
            self::error('修改失败');
        }
    }


    public static function delete(array $params)
    {
        if(self::builder_query(['cursors_id'=>self::deleteParamId($params)])->delete() === false){
            self::error('删除失败');
        }
    }
}
